package com.mmdccj.yugu.controller;

import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.common.Result;
import com.mmdccj.yugu.dto.MusicDto;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.dto.PlayDto;
import com.mmdccj.yugu.entity.Music;
import com.mmdccj.yugu.entity.User;
import com.mmdccj.yugu.service.MusicService;
import com.mmdccj.yugu.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MMDCCJ
 * @date: 2023-11-27 23:55
 */
@Slf4j
@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    private MusicService musicService;
    @Autowired
    private UserService userService;

    /**
     * 获取歌单中歌曲列表 与 歌曲 歌手 信息
     *
     * @return Result<MusicListDto>
     */
    @GetMapping("/getList")
    public Result<MusicListDto> getList(String musicListId) {
        if (musicListId == null) return Result.error("参数为空");
        log.info("获取歌单{}", musicListId);
        MusicListDto dto = musicService.getMusicByMusicList(musicListId);
        if (dto == null) return Result.error("不存在的歌单id");
        String userName = userService.getUserNameById(dto.getCreatorId());
        if (userName == null) {
            return Result.success("成功获取歌单音乐,获取用户失败", dto);
        }
        List<User> sortUsers = new ArrayList<>();
        long stime = System.currentTimeMillis();
        dto.getMusicList().forEach((item) -> {
            sortUsers.add(userService.getUserByMusicId(item.getMusicId()));
        });
        dto.setSingerList(sortUsers);
        dto.setCreatorName(userName);
        long etime = System.currentTimeMillis();
        log.info("执行时长{} 毫秒.", (etime - stime));
        return Result.success("成功获取歌单音乐", dto);
    }

    /**
     * 获取单曲
     *
     * @param musicId
     * @return Result<MusicDto>
     */
    @GetMapping("/getMusic")
    public Result<MusicDto> getMusic(String musicId) {
        if (musicId == null) return Result.error("参数缺失");
        MusicDto musicDto = musicService.getMusicAndSingerById(musicId);
        return Result.success("成功", musicDto);
    }

    /**
     * 获取歌手的所有单曲
     *
     * @param userId
     * @return Result<List < Music>>
     */
    @GetMapping("/singer")
    public Result<List<Music>> singer(String userId) {
        if (userId == null) return Result.error("参数缺失");
        return Result.success("成功", musicService.getSingerAllMusicById(userId));
    }

    /**
     * 更新播放次数
     *
     * @param session session
     * @param musicId musicId
     * @param typeId  typeId
     * @param type    type
     * @return Result<String> 结果
     */
    @PostMapping("/addPlayTimes")
    public Result<String> addPlayTimes(HttpSession session, String musicId, String typeId, String type) {
        if (musicId == null || type == null) {
            return Result.error("缺少参数");
        }
        String userId = (String) session.getAttribute("user");
        musicService.updatePlayTimes(userId, musicId, typeId, type);
        return Result.success("成功");
    }

    /**
     * 获取一周内播放记录排行 （10条）
     *
     * @param session session
     * @param id      用户id 不是必传 - 当获取他人数据是才需
     * @return Result<List < PlayDto>>
     */
    @GetMapping("/getWeekPlay")
    public Result<List<PlayDto>> getWeekPlay(HttpSession session, String id) {
        String userId;
        if (id == null) {
            userId = (String) session.getAttribute("user");
        } else {
            userId = id;
        }

        if (userId == null) {
            return Result.error("缺少参数");
        }
        return Result.success("成功", musicService.getUserWeekPlay(userId));
    }

    /**
     * 上传新歌
     *
     * @param session  session
     * @param duration 持续时间
     * @param title    歌曲title
     * @param music    歌曲文件信息
     * @param image    歌曲封面信息
     * @param intro    歌曲介绍信息
     * @return
     */
    @PostMapping("/uploadNewMusic")
    public Result<String> uploadNewMusic(HttpSession session, Integer duration, String title, String music, String image, String intro, String style) {
        if (duration != null && title != null && music != null && image != null && intro != null && style != null) {
            String userId = (String) session.getAttribute("user");
            if (userId == null) return Result.error("未登录");
            musicService.insertMusic(userId, duration, title, music, image, intro, style);
            return Result.success("成功");
        } else {
            return Result.error("缺少参数");
        }
    }

    /**
     * 获取用户上传的音乐
     *
     * @param session session
     * @return Result<List < Music>>
     */
    @GetMapping("/getUserMusic")
    public Result<List<Music>> getUserMusic(HttpSession session) {
        String userId = (String) session.getAttribute("user");
        if (userId != null) {
            List<Music> list = musicService.getUserMusic(userId);
            if (list == null) {
                return Result.error("结果为空");
            }
            return Result.success("成功获取我的音乐", list);
        } else {
            return Result.error("错误");
        }
    }

    @GetMapping("/editInfo")
    public Result<Music> editInfo(HttpSession session, String musicId) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) return Result.error("未登录");
        Music music = musicService.getMusicById(musicId);
        if (music == null) {
            return Result.error("歌曲信息为空");
        }
        return Result.success("成功", music);
    }

    @PostMapping("/updateMusic")
    public Result<String> updateMusic(HttpSession session, String musicId, Integer duration, String title, String music, String image, String intro, String style) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) return Result.error("未登录");
        musicService.updateMusic(musicId, duration, title, music, image, intro, style);
        return Result.success("成功");
    }

    /**
     * 获取用户未被添加到CD中的单曲，按理说一首歌只能属于一个专辑
     */
    @GetMapping("/getMusicWithoutCD")
    public Result<List<Music>> getMusicWithoutCD(HttpSession session) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) return Result.error("未登录");
        List<Music> musicList = musicService.getUserAllMusicWithoutCD(userId);
        return Result.success("成功", musicList);
    }

    @PostMapping("/deleteSingle")
    public Result<String> deleteSingle(HttpSession session, String musicId) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) return Result.error("未登录");
        if (musicId == null) return Result.error("参数缺失");
        musicService.deleteSingle(userId, musicId);
        return Result.success("成功删除");
    }

    @GetMapping("/getAllMusic")
    public Result<PageInfo<MusicDto>> getAllMusic(Integer currentPage) {
        if (currentPage == null) {
            return Result.error("参数缺失");
        }
        return Result.success("成功", musicService.getAllMusicDto(currentPage));
    }

    @GetMapping("/allPlaysNums")
    public Result<Integer> allPlayNums(HttpSession session, String userId) {
        if (userId == null) {
            userId = (String) session.getAttribute("user");
        } else {
            return Result.success("成功", musicService.getAllPlayNums(userId));
        }
        return Result.error("未登录");
    }

    @GetMapping("/getMostListMusic")
    public Result<List<MusicDto>> getMostListMusic() {
        return Result.success("成功", musicService.getMostLikeMusic());
    }

    @GetMapping("/getNewMusic")
    public Result<List<MusicDto>> getNewMusic() {
        return Result.success("成功", musicService.getNewMusic());
    }
}
